<?php

class Own extends fvRoot{
    
    protected $currentEntity = '';
    
    function __construct () {
        $this->currentEntity = __CLASS__;
        parent::__construct(fvSite::$fvConfig->get("entities.{$this->currentEntity}.fields"), 
                            fvSite::$fvConfig->get("entities.{$this->currentEntity}.table_name"), 
                            fvSite::$fvConfig->get("entities.{$this->currentEntity}.primary_key", "id"));
    }
    
    function validateName($value) {
        $valid = (strlen($value) > 0);
        $this->setValidationResult('name', $valid);
        return $valid;
    }
    
    function validateAddress($value) {
        $valid = (strlen($value) > 0);
        $this->setValidationResult('address', $valid);
        return $valid;
    }
    
    function validateLogin($value) {
        $valid = (preg_match("/^[a-z_\s0-9]{4,}$/i", $value) > 0);
        $this->setValidationResult("login", $valid);
        return $valid;
    }
    
    function validatePassword($value) {
        
        $valid = (preg_match("/^[a-z_\s0-9]{4,}$/i", $value) > 0);
        $this->setValidationResult("password", $valid);
        
        $m = fvRequest::getInstance()->getRequestParameter("m");
        $confirmPassword = $m['password1'];
        
        $valid = $valid && ($confirmPassword == $value);
        $this->setValidationResult("password1", ($confirmPassword == $value), "Пароль и подтверждение не совпадают");
        
        return $valid;
    }
    
    function getFullName() {
        return $this->name;
    }
    
    public function getLogMessage($operation) {
        $message = "Агенство было ";
        switch ($operation) {
            case Log::OPERATION_INSERT: $message .= "создано ";break;
            case Log::OPERATION_UPDATE: $message .= "изменено ";break;
            case Log::OPERATION_DELETE: $message .= "удалено ";break;
            case Log::OPERATION_ERROR: $message = "Произошла ошибка при операции с записью ";break;
        }
        $user = fvSite::$fvSession->getUser();
        if ($user)
        $message .= "в ".date("Y-m-d H:i:s").". Менеджер [".$user->getPk()."] " . $user->getLogin() . " (" . $user->getFullName() . ")";
        else 
		$message .= "в ".date("Y-m-d H:i:s").".";        
        return $message;    
    }
    
    public function getLogName() {
        return $this->name;    
    }
    
    public function putToLog($operation) {
        $logMessage = new Log();
        $logMessage->operation = $operation;
        $logMessage->object_type = __CLASS__;
        $logMessage->object_name = $this->getLogName();
        $logMessage->object_id = $this->getPk();
        $logMessage->manager_id = (fvSite::$fvSession->getUser())?fvSite::$fvSession->getUser()->getPk():-1;
        $logMessage->message = $this->getLogMessage($operation);
        $logMessage->edit_link = fvSite::$fvConfig->get('dir_web_root')."own/edit/?id=".$this->getPk();
        $logMessage->save();
    }
}

?>
